package com.yx.cloud.product.laser.tuyapublic;


import android.util.Log;
import com.google.gson.Gson;
import com.iot.common.utils.StringUtils;
import com.iot.product.sweeper.bean.Appointment;
import com.iot.product.sweeper.bean.CleanParam;
import com.iot.product.sweeper.bean.QuietHour;
import com.iot.product.sweeper.bean.Robot;
import com.iot.product.sweeper.bean.RobotStatus;
import com.iot.common.utils.Event;
import com.iot.common.utils.LogUtils;
import com.iot.product.sweeper.bean.RoomCleanParam;
import com.iot.sweeper.AppContext;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class TuyaLaserDataConverter {
    private final static String TAG = "TuyaPublicDataConverter";

    public static void parseDp(String dps) {
        initRobotData(dps);
    }

    private static void initRobotData(String dps) {
        Robot robot = AppContext.get().getCurrentDevice();
        LogUtils.i("aaaaaaaaaa 公版 DPParser dps: " + dps);
        try {
            JSONObject jsonObject = new JSONObject(dps);
            if (jsonObject.has(TuyaLaserConstant.SWITCH_GO)) {
                //清扫开关 true：清扫启动 false：清扫停止
                boolean isSweeping = jsonObject.getBoolean(TuyaLaserConstant.SWITCH_GO);
                Log.i(TAG, "DP_ID_POWER_GO-" + " : " + isSweeping);
            }
            if (jsonObject.has(TuyaLaserConstant.PAUSE)) {
                //工作模式true：暂停当前工作状态 false：继续当前工作状态
                boolean mode = jsonObject.getBoolean(TuyaLaserConstant.PAUSE);
            }
            if (jsonObject.has(TuyaLaserConstant.SWITCH_CHARGE)) {
                //回充开关true：回充启动false：回充停止
                String mode = jsonObject.getString(TuyaLaserConstant.SWITCH_CHARGE);
            }
            if (jsonObject.has(TuyaLaserConstant.MODE)) {
                //工作模式默认值（不可修改删除，否则影响控制面板逻辑）：
                //smart - 自动清扫模式/全屋清扫模式
                //chargego - 自动回充模式（需要兼容：goto_charge）
                //zone - 划区清扫模式/矩形清扫模式
                //pose - 指哪扫哪模式/定点清扫模式
                //part - 局部清扫模式
                //select_room - 选区清扫模式
                int workMode = robot.getRobotStatus().getWorkMode();
                String clean_mode = jsonObject.getString(TuyaLaserConstant.MODE);
                if ("smart".equals(clean_mode)) {
                    //全局清扫
                    workMode = RobotStatus.MODE_AUTO_CLEAN;
                } else if ("pose".equals(clean_mode)) {
                    //指哪扫哪(定点扫)
                    workMode = RobotStatus.MODE_SPOT_CLEAN;//定点清扫
                } else if ("zone".equals(clean_mode)) {
                    //区域扫
                    workMode = RobotStatus.MODE_CUSTOM_CLEAN;
                } else if ("chargego".equals(clean_mode) || "goto_charge".equals(clean_mode)) {
                    //回充
                    workMode = RobotStatus.MODE_RECHARGE;
                } else if ("select_room".equals(clean_mode)) {
                    //选区清扫
                    workMode = RobotStatus.MODE_ROOM_CLEAN;//按房间清扫
                } else if ("part".equals(clean_mode)) {
                    //局部清扫
                    workMode = RobotStatus.MODE_SPOT_CLEAN;//局部清扫
                } else if ("wall_follow".equals(clean_mode)) {
                    //沿墙
                    workMode = RobotStatus.MODE_EDGE_CLEAN;//沿墙清扫
                }
                // robot.getRobotStatus().setWorkState(RobotStatus.STATE_WORKING);//状态有延时，这时取到的还是上次的状态，就出现状态模式不匹配的问题
                robot.getRobotStatus().setWorkMode(workMode);
            }

            if (jsonObject.has(TuyaLaserConstant.STATUS)) {
                String robot_state = jsonObject.getString(TuyaLaserConstant.STATUS);
                LogUtils.d("aaaaaaaaaa 状态---- : " + robot_state);// TODO：

                parseState(robot_state, robot);
            }
            if (jsonObject.has(TuyaLaserConstant.CLEAN_TIME)) {
                ///实时 清扫时间
                int clean_time = jsonObject.getInt(TuyaLaserConstant.CLEAN_TIME);
                robot.getCleanStatistics().setCleanTime((int) clean_time);
                Event.newBuilder().type(Event.MESSAGE_TYPE_CLEAN_AREA_DURATION).send();
            }
            if (jsonObject.has(TuyaLaserConstant.CLEAN_AREA)) {
                ///实时 清扫面积
                int clean_area = jsonObject.getInt(TuyaLaserConstant.CLEAN_AREA);
                robot.getCleanStatistics().setCleanArea((int) clean_area);
                Event.newBuilder().type(Event.MESSAGE_TYPE_CLEAN_AREA_DURATION).send();
            }

            if (jsonObject.has(TuyaLaserConstant.RESIDUAL_ELECTRICITY)) {
                //电量
                int battery = jsonObject.getInt(TuyaLaserConstant.RESIDUAL_ELECTRICITY);
                Log.d(TAG, "8 电量---- : " + battery);
                robot.getRobotStatus().setBattery(battery);
                Event.newBuilder().type(Event.MSG_ROBOT_STATE_POWER).content(battery).send();
                //低电量
                if (battery < 20) {
                    Event.newBuilder().type(Event.MSG_ROBOT_STATE_LOW_POWER).content(battery).send();
                } else {
                    Event.newBuilder().type(Event.MSG_ROBOT_STATE_HEALTH_POWER).content(battery).send();
                }
            }
            if (jsonObject.has(TuyaLaserConstant.SUCTION)) {
                //吸力 closed - 关闭 gentle - 安静 normal - 正常 strong - 强劲 max - 超强
                String fan_mode = jsonObject.getString(TuyaLaserConstant.SUCTION);
                Log.d(TAG, "9 吸力---- : " + fan_mode);
                int suction = 0;
                if ("closed".equals(fan_mode)) {
                    suction = 0;
                } else if ("gentle".equals(fan_mode)) {
                    suction = 1;
                } else if ("normal".equals(fan_mode)) {
                    suction = 2;
                } else if ("strong".equals(fan_mode)) {
                    suction = 3;
                } else if ("max".equals(fan_mode)) {
                    suction = 4;
                }
                robot.getCleanSettings().setSuction(suction);
                Event.newBuilder().result(true).type(Event.MESSAGE_TYPE_SUCTION_STATUS).content(suction).send();
            }
            if (jsonObject.has(TuyaLaserConstant.CISTERN)) {
                //水量 closed - 关闭 low - 低 middle - 中 high - 高
                String water_mode = jsonObject.getString(TuyaLaserConstant.CISTERN);
                int water = 0;
                //获取水量设置
                if ("closed".equals(water_mode)) {
                    water = 0;
                } else if ("low".equals(water_mode)) {
                    water = 1;
                } else if ("middle".equals(water_mode)) {
                    water = 2;
                } else if ("high".equals(water_mode)) {
                    water = 3;
                }
                robot.getCleanSettings().setMopHumidity(water);
                Event.newBuilder().result(true).type(Event.MESSAGE_TYPE_WATER_STATUS).content(water).send();
            }
            if (jsonObject.has(TuyaLaserConstant.COMMAND_TRANS)) {
                String strData = jsonObject.getString(TuyaLaserConstant.COMMAND_TRANS);
                byte[] bytes = StringUtils.hex2byte(strData);
                List<byte[]> data = TuyaPublicMapDataParser.getCmdAndData(bytes);
                if (data != null && data.size() > 0) {
                    for (int i = 0; i < data.size(); i++) {
                        byte cmd = data.get(i)[0];
                        byte[] dst = new byte[data.get(i).length - 1];
                        System.arraycopy(data.get(i), 1, dst, 0, dst.length);
                        ParseResult result;
                        switch (cmd) {
                            case 0x13://虚拟墙设置上报
                                Log.d(TAG, "aaaaaaaaaa 虚拟墙设置上报");
                                //{"15":"aa00011717aa00032901002aaa00021b001baa000a13010082001e02ee001ec2aa000315000015"}
                                //{"15":"aa00011717aa00141b01000400ce015aa00032901002a101e2004d0128ff890014008cc1aa000a130100c800640186017c44aa000315000015"}
                                result = TuyaPublicMapDataParser.parseFrobidenLineData((TuyaLaserMap) (robot.getMap().getMap()), robot.getMap().getArea(), dst);
                                if (result.getCode() == ParseResult.MSG_PARSE_SUCCESS) {
                                    Log.d(TAG, "aaaaaaaaaa 虚拟墙设置上报解析成功");
                                    Event.newBuilder().type(Event.MSG_ROBOT_AREA).result(true).content(robot.getMap().getArea()).send();
                                    if (!jsonObject.has("1")) {
                                        Event.newBuilder().type(Event.MESSAGE_TYPE_SET_AREA_SUCCESS).result(true).send();
                                    }
                                }
                                break;
                            case 0x15://选区清扫上报
                                break;
                            case 0x17://定点清扫上报
                                break;
                            case 0x1B://禁区设置上报
                                Log.d(TAG, "aaaaaaaaaa 禁区设置上报");
                                result = TuyaPublicMapDataParser.parseFrobidenAreaData((TuyaLaserMap) (robot.getMap().getMap()), robot.getMap().getArea(), dst);
                                if (result.getCode() == ParseResult.MSG_PARSE_SUCCESS) {
                                    Log.d(TAG, "aaaaaaaaaa 禁区设置上报解析成功");
                                    Event.newBuilder().type(Event.MSG_ROBOT_AREA).result(true).content(robot.getMap().getArea()).send();
                                    if (!jsonObject.has("1")) {
                                        Event.newBuilder().type(Event.MESSAGE_TYPE_SET_AREA_SUCCESS).result(true).send();
                                    }
                                }
                                break;
                            case 0x1D://房间分割上报
                                Log.d(TAG, "aaaaaaaaaa 房间分割上报");
                                if (dst[0] == 0x01) {
                                    Log.d(TAG, "aaaaaaaaaa 房间分割成功");
                                    Event.newBuilder().type(Event.MESSAGE_TYPE_DIVIDE_AREA_SUCCESS).result(true).send();
                                }
                                if (dst[0] == 0x00) {
                                    Log.d(TAG, "aaaaaaaaaa 房间分割失敗");
                                    Event.newBuilder().type(Event.MESSAGE_TYPE_DIVIDE_AREA_FAIL).result(true).send();
                                }
                                break;
                            case 0x1F://房间合并上报
                                Log.d(TAG, "aaaaaaaaaa 房间合并上报");
                                if (dst[0] == 0x01) {
                                    Log.d(TAG, "aaaaaaaaaa 房间合并成功");
                                    Event.newBuilder().type(Event.MESSAGE_TYPE_MERGE_AREA_SUCCESS).result(true).send();
                                }
                                if (dst[0] == 0x00) {
                                    Log.d(TAG, "aaaaaaaaaa 房间合并失敗");
                                    Event.newBuilder().type(Event.MESSAGE_TYPE_MERGE_AREA_FAIL).result(false).send();
                                }
                                if (dst[0] == 0x02) {
                                    Log.d(TAG, "aaaaaaaaaa 房间不相邻");
                                    Event.newBuilder().type(Event.MESSAGE_TYPE_TWO_ROOM_CAN_NOT_REACH).result(true).send();
                                }
                                break;
                            case 0x21://房间分区恢复默认上报
                                if (dst[0] == 0x01) {
                                    Log.d(TAG, "aaaaaaaaaa 房间分区恢复默认成功");
                                    Event.newBuilder().type(Event.MESSAGE_TYPE_RESET_ROOM_SUCCESS).result(true).send();
                                }
                                if (dst[0] == 0x00) {
                                    Log.d(TAG, "aaaaaaaaaa 房间分区恢复默认失敗");
                                    Event.newBuilder().type(Event.MESSAGE_TYPE_RESET_ROOM_FAIL).result(false).send();
                                }
                                break;
                            case 0x23://定制模式保存上报
                                //"aa000c230202ffffff0103ffffff0126"
                                result = TuyaPublicMapDataParser.parseCustomInfoData(robot.getMap().getRoom(), dst);
                                if (result.getCode() == ParseResult.MSG_PARSE_SUCCESS) {
                                    Event.newBuilder().type(Event.MESSAGE_TYPE_SAVE_CUSTOM_SUCCESS).result(true).send();
                                }
                                break;
                            case 0x25://房间重命名上报
                                Log.d(TAG, "aaaaaaaaaa 房间重命名上报");
                                result = TuyaPublicMapDataParser.parseRoomNameData(robot.getMap().getRoom(), dst);
                                if (result.getCode() == ParseResult.MSG_PARSE_SUCCESS) {
                                    Log.d(TAG, "aaaaaaaaaa 房间重命名上报成功");
                                    Event.newBuilder().type(Event.MESSAGE_TYPE_RENAME_AREA_SUCCESS).result(true).send();
                                }
                                break;
                            case 0x27://设置房间清扫顺序上报
                                break;
                            case 0x29://划区清扫上报
                                result = TuyaPublicMapDataParser.parseCleanAreaData((TuyaLaserMap) (robot.getMap().getMap()), robot.getMap().getArea(), dst);
                                if (result.getCode() == ParseResult.MSG_PARSE_SUCCESS) {
                                    Event.newBuilder().type(Event.MSG_ROBOT_AREA).result(true).content(robot.getMap().getArea()).send();
                                    Log.d(TAG, "划区清扫上报");
                                }
                                break;
                            case 0x2B:
                                if (dst[0] == 0x01) {
                                    Event.newBuilder().type(Event.MESSAGE_TYPE_SAVE_MAP_SUCCESS).result(true).send();
                                    Log.d(TAG, "save map success");
                                }
                                if (dst[0] == 0x00) {
                                    Event.newBuilder().type(Event.MESSAGE_TYPE_SAVE_MAP_FAIL).result(true).send();
                                    Log.d(TAG, "save map fail");
                                }
                                if (dst[0] == 0x02) {
                                    Event.newBuilder().type(Event.MESSAGE_TYPE_SAVE_MAP_FAIL).result(true).send();
                                    Log.d(TAG, "save map no room to save");
                                }
                                break;
                            case 0x2D:
                                if (dst[0] == 0x01) {
                                    Event.newBuilder().type(Event.MESSAGE_TYPE_DELETE_MULTI_MAP).result(true).send();
                                    Log.d(TAG, "delete map success");
                                }
                                if (dst[0] == 0x00) {
                                    Event.newBuilder().type(Event.MESSAGE_TYPE_DELETE_MULTI_MAP).result(false).send();
                                    Log.d(TAG, "delete map fail");
                                }
                                break;
                            case 0x2F:
                                Log.d(TAG, "aaaaaaaaaa setCurrent map");
                                if (dst[0] == 0x01) {
                                    Event.newBuilder().type(Event.MESSAGE_TYPE_SET_MAP_SUCCESS).result(true).send();
                                    Log.d(TAG, "setCurrent map success");
                                    Log.d(TAG, "aaaaaaaaaa setCurrent map success");
                                }
                                if (dst[0] == 0x00) {
                                    Event.newBuilder().type(Event.MESSAGE_TYPE_SET_MAP_FAIL).result(true).send();
                                    Log.d(TAG, "setCurrent map fail");
                                    Log.d(TAG, "aaaaaaaaaa setCurrent map fail");
                                }
                                break;
                            case (byte) 0x9B:
                                Log.d(TAG, "aaaaaaaaaa 障碍物/家具上报");
                                result = TuyaPublicMapDataParser.parseObstacleAndFurnitureData((TuyaLaserMap) (robot.getMap().getMap()), robot.getMap().getFurniture(), robot.getMap().getObjectList(), dst);
                                if (result.getCode() == ParseResult.MSG_PARSE_SUCCESS) {
                                    Log.d(TAG, "aaaaaaaaaa 障碍物/家具上报上报解析成功");
                                    Event.newBuilder().type(Event.MSG_ROBOT_FURNITURE).result(true).content(robot.getMap().getFurniture()).send();
                                    Event.newBuilder().type(Event.MSG_ROBOT_OBJECT).result(true).content(robot.getMap().getObjectList()).send();
                                }
                                break;
                            default:
                                break;
                        }
                    }
                }
            }
            if (jsonObject.has(TuyaLaserConstant.EDGE_BRUSH)) {
                //边刷寿命
                int edge_brush = jsonObject.getInt(TuyaLaserConstant.EDGE_BRUSH) * 60;
                robot.getConsumption().setSideBrushLife(edge_brush);
                Event.newBuilder().result(true).type(Event.MESSAGE_TYPE_CONSUMPTION_UPDATE).content("X3").send();
            }
            if (jsonObject.has(TuyaLaserConstant.ROLL_BRUSH)) {
                //滚刷寿命
                int roll_brush = jsonObject.getInt(TuyaLaserConstant.ROLL_BRUSH) * 60;
                robot.getConsumption().setMainBrushLife(roll_brush);
                Event.newBuilder().result(true).type(Event.MESSAGE_TYPE_CONSUMPTION_UPDATE).content("X3").send();
            }
            if (jsonObject.has(TuyaLaserConstant.FILTER)) {
                //滤网寿命
                int strainerLife = jsonObject.getInt(TuyaLaserConstant.FILTER) * 60;
                robot.getConsumption().setStrainerLife(strainerLife);
                Event.newBuilder().result(true).type(Event.MESSAGE_TYPE_CONSUMPTION_UPDATE).content("X3").send();
            }
            if (jsonObject.has(TuyaLaserConstant.RAG_LIFE)) {
                //拖布寿命
                int mopLife = jsonObject.getInt(TuyaLaserConstant.RAG_LIFE) * 60;
                Log.d("disturb", "mopLife is " + mopLife);
                robot.getConsumption().setMopLife(mopLife);
                Event.newBuilder().result(true).type(Event.MESSAGE_TYPE_CONSUMPTION_UPDATE).content("X3").send();
            }
            if (jsonObject.has(TuyaLaserConstant.RAG_LIFE)) {
                //拖布寿命
                int mopLife = jsonObject.getInt(TuyaLaserConstant.RAG_LIFE) * 60;
                Log.d("disturb", "mopLife is " + mopLife);
                robot.getConsumption().setMopLife(mopLife);
                Event.newBuilder().result(true).type(Event.MESSAGE_TYPE_CONSUMPTION_UPDATE).content("X3").send();
            }
            if (jsonObject.has(TuyaLaserConstant.RAG_LIFE_RESET)) {
                //拖布重置
                boolean filterReset = jsonObject.getBoolean(TuyaLaserConstant.RAG_LIFE_RESET);
            }
            if (jsonObject.has(TuyaLaserConstant.SWITCH_DISTURB)) {
                //勿扰开关
                boolean quiteHourSwitch = jsonObject.getBoolean(TuyaLaserConstant.SWITCH_DISTURB);
                List<QuietHour> quietHourList = robot.getQuietHourList();
                if (quietHourList.size() > 0) {
                    quietHourList.get(0).setEnable(quiteHourSwitch);
                } else {
                    QuietHour quietHour = new QuietHour();
                    quietHour.setEnable(quiteHourSwitch);
                    quietHour.setRepeat(Arrays.asList(0, 1, 2, 3, 4, 5, 6));
                    quietHour.setStartTime(20 * 3600 + 0 * 60);
                    quietHour.setEndTime(8 * 3600 + 0 * 60);
                    quietHourList.add(quietHour);
                }
                Event.newBuilder().result(true).type(Event.MESSAGE_TYPE_GET_NO_DISTURB).content(quiteHourSwitch).send();
            }
            if (jsonObject.has(TuyaLaserConstant.VOLUME_SET)) {
                //音量
                int volume = jsonObject.getInt(TuyaLaserConstant.VOLUME_SET);
                robot.getCleanSettings().setVolume(volume);
                Event.newBuilder().result(true).type(Event.MSG_ROBOT_SETTINGS).content(volume).send();
            }
            if (jsonObject.has(TuyaLaserConstant.BREAK_CLEAN)) {
                //断点续扫
                boolean breakpoint = jsonObject.getBoolean(TuyaLaserConstant.BREAK_CLEAN);
                robot.getCleanSettings().setBreakpointResume(breakpoint);
            }
            if (jsonObject.has(TuyaLaserConstant.FAULT)) {
                //故障告警 edge_sweep_fault - 边刷故障 middle_sweep_fault - 滚刷故障 left_wheel_fault - 左轮故障
                //right_wheel_fault - 右轮故障 garbage_box_fault - 尘盒故障 land_check_fault - 地检故障 collision_fault - 碰撞传感器故障
                int fault = jsonObject.getInt(TuyaLaserConstant.FAULT);
                //如果是错误状态 显示异常的条条， 如果不是 隐藏它
                //初始化故障信息
                robot.getRobotStatus().setError(fault + "");
//                robot.getRobotStatus().setWorkState(RobotStatus.STATE_ERROR_28);
                Event.newBuilder().type(Event.MESSAGE_TYPE_FAULT_STATUS).result(true).send();
            }
            if (jsonObject.has(TuyaLaserConstant.CLEAN_AREA_TOTAL)) {
                //总清扫面积
                int total_clean_area = jsonObject.getInt(TuyaLaserConstant.CLEAN_AREA_TOTAL);
                robot.getCleanStatistics().setHistoryArea(total_clean_area);
                Log.d(TAG, "总清扫面积: " + total_clean_area);
                Event.newBuilder().type(Event.MESSAGE_TYPE_CLEAN_AREA_DURATION_FREQUENCY).result(true).send();
            }
            if (jsonObject.has(TuyaLaserConstant.CLEAN_COUNT_TOTAL)) {
                //总清扫次数
                int total_clean_count = jsonObject.getInt(TuyaLaserConstant.CLEAN_COUNT_TOTAL);
                robot.getCleanStatistics().setCleanCount(total_clean_count);
                Log.d(TAG, "总清扫次数: " + total_clean_count);
                Event.newBuilder().type(Event.MESSAGE_TYPE_CLEAN_AREA_DURATION_FREQUENCY).result(true).send();
            }
            if (jsonObject.has(TuyaLaserConstant.CLEAN_TIME_TOTAL)) {
                //总清扫时间  涂鸦公版 时间单位是分钟min 转成秒s
                int total_clean_time = jsonObject.getInt(TuyaLaserConstant.CLEAN_TIME_TOTAL) * 60;
                robot.getCleanStatistics().setHistoryTime(total_clean_time);
                Log.d(TAG, "总清扫时间: " + total_clean_time);
                Event.newBuilder().type(Event.MESSAGE_TYPE_CLEAN_AREA_DURATION_FREQUENCY).result(true).send();
            }
            if (jsonObject.has(TuyaLaserConstant.CLEAN_THE_LOCAL_TIME)) {
                //本地定时任务
                String local_time_task_data = jsonObject.getString(TuyaLaserConstant.CLEAN_THE_LOCAL_TIME);
                String local_time_task = StringUtils.hexStr2Str(local_time_task_data);
                Log.d(TAG, "32 本地定时任务---- : " + local_time_task);
                Log.d(TAG, "32 本地定时任务---- : " + local_time_task_data);

                byte[] bytes = StringUtils.hex2byte(local_time_task_data);
                List<byte[]> data = TuyaPublicMapDataParser.getCmdAndData(bytes);
                Log.d("x333", "data size is " + data.size());
                if (data != null && data.size() > 0) {
                    for (int i = 0; i < data.size(); i++) {
                        String sumData = StringUtils.bytesToHexString(data.get(i));
                        Log.d("x333", "sumdata" + i + " is " + sumData);
                        byte cmd = data.get(i)[0];
                        Log.d("x333", "data" + i + "size is " + data.get(i).length + "=====" + "cmd is " + cmd);
                        byte[] dst = new byte[data.get(i).length - 1];
                        int start = 1;
                        System.arraycopy(data.get(i), 1, dst, 0, dst.length);
                        switch (cmd) {
                            case 0x31://本地定时任务
                                List<Appointment> mList = new ArrayList<>();

                                int appointmentNum = TuyaPublicMapDataParser.getValueFromData(dst, start, 1);
                                Log.d("appointment", "appointmentNum is " + appointmentNum);
                                start += 1;
                                if (appointmentNum >= 1) {
                                    for (int num = 0; num < appointmentNum; num++) {
                                        Appointment appointment = new Appointment();
                                        int appointmentEnable = TuyaPublicMapDataParser.getValueFromData(dst, start, 1);
                                        start += 1;
                                        Log.d("appointment", "appointmentEnable is " + appointmentEnable);
                                        if (appointmentEnable == 1) {
                                            appointment.setEnable(true);
                                        } else if (appointmentEnable == 0) {
                                            appointment.setEnable(false);
                                        } else if (appointmentEnable == 2) {
                                            appointment.setAppointmentExpired(true);
                                            Log.d("appointment", "appointmentEnable is " + appointment.isAppointmentExpired());
                                        }
                                        //重复次数，按位取
                                        byte repeat = dst[start];
                                        start += 1;
                                        List<Integer> weeks = new ArrayList<>();
                                        String strRepeat = StringUtils.byteToBit(repeat);
                                        String[] strings = strRepeat.split("");
                                        for (int index = strings.length - 1; index > 0; index--) {
                                            if (strings[index].equals("1")) {
                                                if (index != 1) {
                                                    weeks.add(8 - index);
                                                } else {
                                                    weeks.add(0);
                                                }

                                            }
                                        }

                                        Log.d("appointment", "strings size " + strings.length);
                                        appointment.setRepeat(weeks);
                                        Log.d("appointment", "repeat is " + strRepeat);
                                        //"aa001531080200400a120000000101010a1200000001fe01b6"
                                        int startHour = TuyaPublicMapDataParser.getValueFromData(dst, start, 1);
                                        Log.d("appointment", "startHour is " + startHour);
                                        start += 1;
                                        int startMin = TuyaPublicMapDataParser.getValueFromData(dst, start, 1);
                                        Log.d("appointment", "startMin is " + startMin);
                                        start += 1;
                                        appointment.setTime(startHour * 3600 + startMin * 60);
                                        appointment.initId();
                                        int roomCount = TuyaPublicMapDataParser.getValueFromData(dst, start, 1);
                                        Log.d("appointment", "roomCount is " + roomCount);
                                        start += 1;
                                        if (roomCount == 0) {
                                            Log.d("appointment", "dst len is " + dst.length);
                                            Log.d("appointment", "start  is " + start);
                                            CleanParam param = new CleanParam();
                                            if (start >= dst.length) {
                                                break;
                                            }
                                            int workMode = TuyaPublicMapDataParser.getValueFromData(dst, start, 1);
                                            appointment.setWorkMode(workMode);
                                            appointment.setCleanMode(0);
                                            Log.d("appointment", "workMode is " + workMode);
                                            start += 1;

                                            int suction = TuyaPublicMapDataParser.getValueFromData(dst, start, 1);
                                            param.suction = suction;
                                            start += 1;
                                            Log.d("appointment", "suction is " + suction);
                                            int humidity = TuyaPublicMapDataParser.getValueFromData(dst, start, 1);
                                            param.humidity = humidity;
                                            start += 1;
                                            Log.d("appointment", "humidity is " + humidity);
                                            int cleanTimes = TuyaPublicMapDataParser.getValueFromData(dst, start, 1);
                                            start += 1;
                                            Log.d("appointment", "cleanTimes is " + cleanTimes);
                                            param.cleanTimes = cleanTimes;
                                            appointment.setCleanParam(param);
                                        } else if (roomCount > 0) {
                                            RoomCleanParam param = new RoomCleanParam();
                                            appointment.setCleanMode(1);
                                            List<Integer> roomIds = new ArrayList<>();
                                            for (int j = 0; j < roomCount; j++) {
                                                int roomId = TuyaPublicMapDataParser.getValueFromData(dst, start, 1);
                                                roomIds.add(roomId);
                                                Log.d("appointment", "roomId is " + roomId);
                                                start += 1;
                                            }
                                            param.setRoomIds(roomIds);
                                            int workMode = TuyaPublicMapDataParser.getValueFromData(dst, start, 1);
                                            appointment.setWorkMode(workMode);
                                            Log.d("appointment", "workMode is " + workMode);
                                            start += 1;

                                            int suction = TuyaPublicMapDataParser.getValueFromData(dst, start, 1);
                                            param.suction = suction;
                                            Log.d("appointment", "suction is " + suction);
                                            start += 1;
                                            int humidity = TuyaPublicMapDataParser.getValueFromData(dst, start, 1);
                                            param.humidity = humidity;
                                            Log.d("appointment", "humidity is " + humidity);
                                            start += 1;
                                            int cleanTimes = TuyaPublicMapDataParser.getValueFromData(dst, start, 1);
                                            param.cleanTimes = cleanTimes;
                                            start += 1;
                                            Log.d("appointment", "cleanTimes is " + cleanTimes);
                                            appointment.setCleanParam(param);
                                        }
                                        mList.add(appointment);
                                    }
                                }
                                robot.setAppointmentList(mList);
                                Log.d("appointment", "mList size is " + mList.size());
                                Log.d("appointment", "mList is " + new Gson().toJson(mList));

                                Event.newBuilder().type(Event.MESSAGE_TYPE_QUERY_ALARM).result(true).content(mList).send();
                                break;
                        }
                    }
                }
            }
            if (jsonObject.has(TuyaLaserConstant.DISTURB_TIME_SET)) {
                //勿扰时间设置
                //aa000833081601000800005a
                String disturb = jsonObject.getString(TuyaLaserConstant.DISTURB_TIME_SET);
                String notDisturb = StringUtils.hexStr2Str(disturb);
                Log.d(TAG, "aaaaaaaaaa 勿扰" + notDisturb);
                Log.d(TAG, disturb + " ： 33 勿扰时间设置---- : " + notDisturb);
                String strData = jsonObject.getString("33");
                byte[] bytes = StringUtils.hex2byte(strData);
                List<byte[]> data = TuyaPublicMapDataParser.getCmdAndData(bytes);
                if (data != null && data.size() > 0) {
                    for (int i = 0; i < data.size(); i++) {
                        byte cmd = data.get(i)[0];
                        byte[] dst = new byte[data.get(i).length - 1];
                        System.arraycopy(data.get(i), 1, dst, 0, dst.length);
                        switch (cmd) {
                            case 0x33://勿扰模式
                                List<QuietHour> quietHourList = robot.getQuietHourList();
                                QuietHour quietHour;
                                int startHour = TuyaPublicMapDataParser.getValueFromData(dst, 1, 1);
                                int startMin = TuyaPublicMapDataParser.getValueFromData(dst, 2, 1);
                                int endHour = TuyaPublicMapDataParser.getValueFromData(dst, 4, 1);
                                int endMin = TuyaPublicMapDataParser.getValueFromData(dst, 5, 1);
                                if (quietHourList.size() > 0) {
                                    quietHour = quietHourList.get(0);
                                } else {
                                    quietHour = new QuietHour();
                                    quietHour.setEnable(false);
                                    quietHour.setRepeat(Arrays.asList(0, 1, 2, 3, 4, 5, 6));
                                    quietHourList.add(quietHour);
                                }
                                quietHour.setStartTime(startHour * 3600 + startMin * 60);
                                quietHour.setEndTime(endHour * 3600 + endMin * 60);
                                Event.newBuilder().type(Event.MESSAGE_TYPE_GET_NO_DISTURB).result(true).content(quietHourList).send();
                                break;
                        }
                    }
                }
            }
            if (jsonObject.has(TuyaLaserConstant.DEVICE_INFO)) {
                //设备信息
                String device_Info = jsonObject.getString(TuyaLaserConstant.DEVICE_INFO);
                String deviceInfo = StringUtils.hexStr2Str(device_Info);
                Log.d(TAG, "34 设备信息---- : " + deviceInfo);
                if (!deviceInfo.equals("") || deviceInfo != null) {
                    JSONObject json = new JSONObject(deviceInfo);
                    String Ip = json.getString("IP");
                    String wifi_name = json.getString("WiFi_Name");
                    String mac = json.getString("Mac");
                    String firmware_version = json.getString("Firmware_Version");
                    String module_uuid = json.getString("Module_UUID");
                    String device_sn = json.getString("Device_SN");

                    robot.getFirmwareInfo().setSsid(wifi_name);
                    robot.getFirmwareInfo().setMac(mac);
                    robot.getFirmwareInfo().setIp(Ip);
                    robot.getFirmwareInfo().setCompileVerions(firmware_version);
                    robot.getFirmwareInfo().setUuid(module_uuid);
                    robot.getFirmwareInfo().setSn(device_sn);
                }

            }
            if (jsonObject.has(TuyaLaserConstant.VOICE_DATA)) {
                //{"35":"ab0000000007350400000001144e"}
                //{"35":"ab0000000007350400000001144e"}
                //{"35":"ab00000000073504000000013c76"}
                //{"35":"ab00000000073504000000000039"}
                //{"35":"ab00000000073504000000013c76"}
                //{"35":"ab0000000007350400000002649f"}
                //{"35":"ab000000000735040000000364a0"}
                //语音包设置设置
                String strData = jsonObject.getString(TuyaLaserConstant.VOICE_DATA);
                Log.d(TAG, "35 语音包设置---- : " + strData);
                byte[] bytes = StringUtils.hex2byte(strData);
                byte[] data = TuyaPublicMapDataParser.getCmdAndData1(bytes);
                byte cmd = data[0];
                byte[] dst = new byte[data.length - 1];
                System.arraycopy(data, 1, dst, 0, dst.length);
                switch (cmd) {
                    case 0x35://勿扰模式
                        int voiceId = TuyaPublicMapDataParser.getValueFromData(dst, 0, 4);
                        int status = TuyaPublicMapDataParser.getValueFromData(dst, 4, 1);
                        int schedu = TuyaPublicMapDataParser.getValueFromData(dst, 5, 1);
                        Log.d(TAG, "35 语音包设置---- status: " + status);
                        Log.d(TAG, "35 语音包设置---- voiceId: " + voiceId);
                        robot.getCleanSettings().setVoiceType("" + voiceId);
                        robot.getCleanSettings().setVoiceBagStatus(status);
                        robot.getCleanSettings().setVoiceBagDownloadProgress(schedu);
                        Event.newBuilder().type(Event.MESSAGE_TYPE_VOICE_STATUS_PROGRESS).result(true).send();
                        break;
                }
            }

            if (jsonObject.has(TuyaLaserConstant.AUTO_BOOST)) {
                //地毯增压(公版)
                boolean auto_boost = jsonObject.getBoolean(TuyaLaserConstant.AUTO_BOOST);
                Log.d(TAG, "45 地毯增压---- : " + auto_boost);
                robot.getCleanSettings().setCarpetPressurize(auto_boost);
                Event.newBuilder().type(Event.MSG_ROBOT_SETTINGS).result(true).send();
            }
            if (jsonObject.has(TuyaLaserConstant.CUSTOM_MODE_SWITCH)) {
                //定制模式
                boolean custom_mode = jsonObject.getBoolean(TuyaLaserConstant.CUSTOM_MODE_SWITCH);
                Log.d(TAG, "105 定制模式---- : " + custom_mode);
                robot.getCleanSettings().setCustomSwitch(custom_mode);
                Event.newBuilder().type(Event.MSG_ROBOT_SETTINGS).result(true).send();
            }
            if (jsonObject.has("37")) {
                /*集尘频率设置 0 - 关闭自动集尘 1 - 1次 2 - 2次 3 - 3次*/
                int dust_collection_num = jsonObject.getInt("37");
                robot.getCleanSettings().setAutoDust(dust_collection_num + "");
                //Event.newBuilder().type(Event.MESSAGE_TYPE_DUST_COLLECTION_NUM).content(dust_collection_num).send();
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    protected static void parseState(String state, Robot robot) {
        Log.d(TAG, "robot_state =state=====" + state);
        /**
         *  standby - 待机中
         *  smart - 自动清扫中
         *  zone_clean - 划区清扫中
         *  part_clean - 局部清扫中
         *  cleaning - 清扫中(备选)
         *  paused - 已暂停
         *  goto_pos - 前往目标点中
         *  pos_arrived - 目标点已到达
         *  pos_unarrive - 目标点不可达
         *  goto_charge - 寻找充电座中
         *  charging - 充电中
         *  charge_done - 充电完成
         *  sleep - 休眠 select_room - 选区清扫中
         *  seek_dust_bucket - 寻找集尘桶中
         *  collecting_dust - 集尘中
         *添加
         *  goto_bs - 返回基站中
         *  cleaning_mop - 清洗拖布中
         *  drying_mop - 风干拖布中
         *
         */
        RobotStatus status = robot.getRobotStatus();
        status.setSubState(RobotStatus.SUBSTATE_NONE);
        if ("sleep".equals(state)) {
            //休眠
            status.setWorkState(RobotStatus.STATE_DORMANT);
        } else if ("standby".equals(state)) {
            //待机
            status.setWorkState(RobotStatus.STATE_STANDBY);
        } else if ("smart".equals(state)) {
            //清扫
            status.setWorkState(RobotStatus.STATE_WORKING);
            status.setWorkMode(RobotStatus.MODE_AUTO_CLEAN);
        } else if ("paused".equals(state)) {
            //暂停
            status.setWorkState(RobotStatus.STATE_PAUSE);
        } else if ("select_room".equals(state)) {
            //选区清扫
            status.setWorkState(RobotStatus.STATE_WORKING);
            status.setWorkMode(RobotStatus.MODE_ROOM_CLEAN);
        } else if ("zone_clean".equals(state)) {
            //区域扫
            status.setWorkState(RobotStatus.STATE_WORKING);
            status.setWorkMode(RobotStatus.MODE_CUSTOM_CLEAN);
        } else if ("part_clean".equals(state) || "pointing".equals(state)) {
            //局部清扫
            status.setWorkState(RobotStatus.STATE_WORKING);
            status.setWorkMode(RobotStatus.MODE_SPOT_CLEAN);
        } else if ("goto_charge".equals(state) || "tocharge".equals(state)) {
            //回充中
            status.setWorkState(RobotStatus.STATE_WORKING);
            status.setWorkMode(RobotStatus.MODE_RECHARGE);
        } else if ("mop".equals(state)) {
            //拖地
            status.setWorkState(RobotStatus.STATE_WORKING);
            status.setWorkMode(RobotStatus.MODE_MOP);
        } else if ("fault".equals(state)) {
            //故障
            status.setWorkState(RobotStatus.STATE_ERROR);
        } else if ("seek_dust_bucket".equals(state)) {
            //寻找集尘桶中
            status.setWorkState(RobotStatus.STATE_DUST_COLLECTION_PUBLIC);
        } else if ("amicro_collect_dust".equals(state) || "collecting_dust".equals(state)) {
            //集尘中  collecting_dust 这个字段适配LF27
            status.setWorkState(RobotStatus.STATE_DUST_COLLECTION);
        } else if ("charging".equals(state) || "charge_done".equals(state)) {
            //充电中
            status.setWorkState(RobotStatus.STATE_CHARGING);
        } else if ("drying_mop".equals(state)) {
            // 风干拖布中
            status.setWorkState(RobotStatus.STATE_DRYING_MOP);
        } else if ("cleaning_mop".equals(state)) {
            // 清洗拖布中
            status.setWorkState(RobotStatus.STATE_CLEANING_MOP);
        } else if ("goto_bs".equals(state)) {
            // 返回基站中
            status.setWorkState(RobotStatus.STATE_GOTO_BS);
        } else if ("goto_pos".equals(state)) {
            // 前往目标点中 状态限制】同清扫
            status.setWorkState(RobotStatus.STATE_WORKING);
            status.setSubState(RobotStatus.SUBSTATE_WORKING_GOTO_POS);
        } else if ("pos_arrived".equals(state)) {
            // 目标点已到达 状态限制】同待机
            status.setWorkState(RobotStatus.STATE_WORKING);
            status.setSubState(RobotStatus.SUBSTATE_WORKING_POS_ARRIVED);
        } else if ("pos_unarrive".equals(state)) {
            // 目标点不可达 状态限制】同待机
            status.setWorkState(RobotStatus.STATE_STANDBY);
            status.setSubState(RobotStatus.SUBSTATE_WORKING_POS_UNARRIVE);
        } else if ("direction_control".equals(state)) {
            // 遥控模式 状态限制】同工作中
            status.setWorkState(RobotStatus.STATE_WORKING);
            status.setWorkMode(RobotStatus.MODE_CONTROL);
        } else if ("repositing".equals(state)) {
            // 重定位中 状态限制】同全屋/自定义...清扫状态
            status.setWorkState(RobotStatus.STATE_WORKING);
            status.setSubState(RobotStatus.SUBSTATE_WORKING_REPOSITING);
        } else if ("creating_map".equals(state)) {
            // 建图中
            status.setWorkState(RobotStatus.STATE_WORKING);
            status.setSubState(RobotStatus.SUBSTATE_WORKING_CREATINGMAP);
        }
//        Event.newBuilder().type(Event.MSG_ROBOT_STATE).send();
    }

}
